package com.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dto.PageDTO;
import com.query.DiscoveryQuery;
import com.service.CommentService;

import com.result.Result;
import com.service.DiscoveryService;
import com.vo.CommentVo;
import com.vo.DiscoveryPageVo;
import com.vo.DiscoveryVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**社区相关接口
 * @param
 * @return
 */
@RestController
@Slf4j
@RequestMapping("/discovery")
@RequiredArgsConstructor
public class DiscoveryController {

    private final DiscoveryService discoveryService;

    private final CommentService commentService;
    /**
     * 上传用户社区帖子
     * @return
     */
    @PostMapping ("/upload")
    public Result uploadDiscovery(@RequestParam(required = false) List<MultipartFile> image,@RequestParam(required = false) List<MultipartFile> video,
                                  @RequestParam List<String> iclazz,@RequestParam String idescribe, @RequestParam String title){
        log.info("用户:{}上传帖子:{}", StpUtil.getLoginIdAsLong());
        discoveryService.saveDiscovery(image,video,iclazz,idescribe,title);
        return Result.success();
    }

    /**
     * 用户点赞/取消点赞
     * @return
     */
    @PutMapping("/like")
    public Result like(@RequestParam Long id){
        discoveryService.like(id);
        return Result.success();
    }

    /**
     * 用户收藏/取消收藏
     * @param id
     * @return
     */
    @PutMapping("/collect")
    public Result collect(@RequestParam Long id) {
        discoveryService.collect(id);
        return Result.success();
    }

    /**
     * 保存主体评论
     * @param id
     * @param image
     * @param comment
     * @return
     */
    @PostMapping("/saveMasterComment")
    public Result saveComment(@RequestParam Long id,@RequestParam(required = false) MultipartFile image,@RequestParam String comment){
        discoveryService.saveComment(id,image,comment);
        return Result.success();
    }

    /**
     * 回复评论
     * @param toId
     * @param masterId
     * @param image
     * @param comment
     * @return
     */
    @PostMapping("/saveSlaveComment")
    public Result saveSlaveComment(@RequestParam Long id,@RequestParam Long masterId,@RequestParam(required = false) Long toId,@RequestParam(required = false) MultipartFile image,@RequestParam String comment){
        discoveryService.saveSlaveComment(id,masterId,toId,image,comment);
        return Result.success();
    }

    /**
     * 获取所有评论
     * @param id
     * @return
     */
    @GetMapping("/getComments")
    public Result<List<CommentVo>> getComment(@RequestParam Long id){
        List<CommentVo>result=commentService.getComments(id);
        return Result.success(result);
    }

    /**
     * 分页查询帖子
     * @param discoveryQuery
     * @return
     */
    @GetMapping("/getPageDiscovery")
     public Result<PageDTO<DiscoveryPageVo>>getPageDiscovery( DiscoveryQuery discoveryQuery){
        log.info("分页查询:{}",discoveryQuery);
         PageDTO<DiscoveryPageVo> result=discoveryService.getDiscoveryQuery(discoveryQuery);
        return Result.success(result);
     }

    /**
     * 获取帖子信息
     * @param id
     * @return
     */
    @GetMapping("/getDiscovery")
     public Result<DiscoveryVo> getDiscovery(@RequestParam Long id){
         log.info("查询帖子详细信息:{}",id);
         DiscoveryVo discoveryVo=discoveryService.getDiscovery(id);
         return Result.success(discoveryVo);
     }

}
